// pages/recharge/recharge.js

const host = require('../../config.js').host;
const network = require('../../utils/network.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    priceList:["20","50","100","200","500"],
    selectIndex:0,
    price:20
  },
selectIndexClick(e){
  let priceList=this.data.priceList
  let selectIndex=this.data.selectIndex
  let price=this.data.price
  this.setData({
    selectIndex:e.currentTarget.dataset.index,
    price:priceList[e.currentTarget.dataset.index]
  });
  
},
inputChange(e){ 
  this.setData({
    price:e.detail.value
  });
},
// 根据卡位码支付
getPayData() {
 
  let that = this; 
  if(that.data.price==0){
    wx.showToast({
      title: '请选择充值金额',
      icon:'none'
    })
    return;
  }
 

 network.requestPost(host + '/weiXinPayment/charge', {
   "totalMoney": this.data.price
 }, res => {  
   
   if (res.data.code == 200) { 
     
     if(res.data.h5PayArgs){
      that.gotopay(res.data.h5PayArgs)
     }else{
       wx.showToast({
         title: '未获取到支付参数' ,
         icon:'none'
       })
     }
     
   } else {
     wx.showToast({
       title: res.data.msg ? res.data.msg : '请求失败',
       icon: "none",
       duration: 1200
     })
   }
 })
},
gotoquestion(){
  wx.navigateTo({
    url: '/pages/question/question',
  })
},
gotopay(e){
 let that=this; 
 wx.requestPayment({
   signType: e.signType,
   timeStamp: e.timeStamp,
   nonceStr: e.nonceStr,
   package: e.package, 
   paySign: e.paySign,
   success (res) { 
     console.log('成功：'+res) 
     wx.showToast({
       title: '支付成功',
       duration: 2000 
     }) 
     setTimeout(() => {
       wx.navigateBack({
         delta: 1,
       })
     }, 2000);
   },
   fail (res) { 
     console.log('失败：'+res)
     wx.showToast({
       title: '支付失败', 
       duration: 2000
     })
   },
   complete (res){
     console.log("完成调用") 
   }
 })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})